package com.kaifamiao.dispatch;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

@WebServlet( "/include" )
public class IncludeServlet extends HttpServlet {

    private Logger logger = Logger.getLogger( this.getClass().getName() ) ;

    @Override
    protected void service( HttpServletRequest request , HttpServletResponse response)
            throws ServletException, IOException {
        logger.info( "向请求对象中设置属性" );
        request.setAttribute( "outer" , "用来包含其它组件的servlet" );
        PrintWriter out = response.getWriter();

        out.println( "<div style='border: 1px solid red;'>");

        String path1 = "/inner" ; // 一个 servlet 的 url-pattern
        // 获得请求调度器(请求派遣器/请求指派器)
        RequestDispatcher dispatcher1 = request.getRequestDispatcher( path1 );
        dispatcher1.include( request , response );

        logger.info( "从请求对象中获取属性:" + request.getAttribute( "inner" ) );

        String path2 = "/WEB-INF/inner.html";
        RequestDispatcher dispatcher2 = request.getRequestDispatcher( path2 );
        dispatcher2.include( request , response );

        String path3 = "/WEB-INF/inner.jsp";
        RequestDispatcher dispatcher3 = request.getRequestDispatcher( path3 );
        dispatcher3.include( request , response );

        out.println( "</div>");

    }
}
